package com.javaking.clanteam.studentutils;

import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;

public class DatePreference extends DialogPreference {
	
	long mDate;
	
	final long DEFAULT_VALUE = Calendar.getInstance().getTimeInMillis();
	
	private DatePicker mDatePicker;

	public DatePreference(Context context, AttributeSet attrs) {
		this(context,attrs,android.R.attr.dialogPreferenceStyle);
	}
	
	public DatePreference(Context context, AttributeSet attrs, int style) {
		super(context,attrs,style);
		
		setDialogLayoutResource(R.layout.dialog_datepicker);
		setDialogTitle("Choose a Date");
		
//		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DatePrefernece, style, 0);
	}
	
//	@Override
//	protected View onCreateDialogView() {
//		return super.onCreateDialogView();
//	}
	
	@Override
	protected void onDialogClosed(boolean positiveResult) {
		if (positiveResult) {
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.YEAR, mDatePicker.getYear());
			cal.set(Calendar.MONTH, mDatePicker.getMonth());
			cal.set(Calendar.DAY_OF_MONTH, mDatePicker.getDayOfMonth());
			mDate = cal.getTimeInMillis();
			persistLong(mDate);
		}
	}
	
	@Override
	protected View onCreateDialogView() {
		View view = super.onCreateDialogView();
		mDatePicker = (DatePicker) view.findViewById(R.id.date);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(mDate);
		mDatePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);
		return view;
	}
	
	@Override
	protected void onSetInitialValue(boolean restorePersistedValue,
			Object defaultValue) {
		mDate = this.getPersistedLong(DEFAULT_VALUE);
		if (!restorePersistedValue) {
			persistLong(mDate);
		} 
	}
	
	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return DEFAULT_VALUE;
	}
	
	@Override
	protected Parcelable onSaveInstanceState() {
	    final Parcelable superState = super.onSaveInstanceState();
	    // Check whether this Preference is persistent (continually saved)
	    if (isPersistent()) {
	        // No need to save instance state since it's persistent, use superclass state
	        return superState;
	    }

	    // Create instance of custom BaseSavedState
	    final SavedState myState = new SavedState(superState);
	    // Set the state's value with the class member that holds current setting value
	    myState.value = mDate;
	    return myState;
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
	    // Check whether we saved the state in onSaveInstanceState
	    if (state == null || !state.getClass().equals(SavedState.class)) {
	        // Didn't save the state, so call superclass
	        super.onRestoreInstanceState(state);
	        return;
	    }

	    // Cast state to custom BaseSavedState and pass to superclass
	    SavedState myState = (SavedState) state;
	    super.onRestoreInstanceState(myState.getSuperState());
	    
	    Calendar cal = Calendar.getInstance();
	    cal.setTimeInMillis(myState.value);
	    DatePicker datePicker = (DatePicker)getDialog().findViewById(R.id.date);
	    datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);
	}
	
	
	private static class SavedState extends BaseSavedState {
	    // Member that holds the setting's value
	    // Change this data type to match the type saved by your Preference
	    long value;

	    public SavedState(Parcelable superState) {
	        super(superState);
	    }

	    public SavedState(Parcel source) {
	        super(source);
	        // Get the current preference's value
	        value = source.readLong();  // Change this to read the appropriate data type
	    }

	    @Override
	    public void writeToParcel(Parcel dest, int flags) {
	        super.writeToParcel(dest, flags);
	        // Write the preference's value
	        dest.writeLong(value);  // Change this to write the appropriate data type
	    }

	    // Standard creator object using an instance of this class
	    @SuppressWarnings("unused")
		public static final Parcelable.Creator<SavedState> CREATOR =
	            new Parcelable.Creator<SavedState>() {

	        public SavedState createFromParcel(Parcel in) {
	            return new SavedState(in);
	        }

	        public SavedState[] newArray(int size) {
	            return new SavedState[size];
	        }
	    };
	}

}
